package bosanova

class Usuario {

    transient springSecurityService
    String      username
    String      password
    boolean     enabled = true
    boolean     accountExpired = false
    boolean     accountLocked = false
    boolean     passwordExpired = false
    Date        Fechacreacion = new Date()
    
    String      identificacion
    String      nombres
    String      apellidos 
    Date        nacimiento
    String      genero
    String      gruposanguineo
    String      direccion
    Pais        nacionalidad
    String      email
    String      celular  
    String      telefono

    static constraints = {
        username (blank: false, unique: true)
        password (blank: false)
        identificacion(nullable:false,blank: false, unique: true)   
        nacionalidad(nullable:true,blank: true) 
        nombres (matches: /^[a-zA-Z][ a-zA-Z]*$/)
        apellidos ( matches: /^[a-zA-Z][ a-zA-Z]*$/)
        nacimiento (max: new Date())
        genero ( inList:["M","F"])
        gruposanguineo ( inList :["O+","O-","A+","A-","B+","B-","AB+","AB-"])
        email (email: true )
        celular ( matches: /^3[012]\d\d{7}/)
        telefono ( matches: /\d{7}/)
    }
    
    

    static mapping = {
        password column: '`password`'
    }

    Set<Rol> getAuthorities() {
        UsuarioRol.findAllByUsuario(this).collect { it.rol } as Set
    }

    def beforeInsert() {
        encodePassword()
    }

    def beforeUpdate() {
        if (isDirty('password')) {
            encodePassword()
        }
    }

    protected void encodePassword() {
        password = springSecurityService.encodePassword(password)
    }
}
